﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.UI.common;

namespace rock.UI.fee
{
    public partial class FormFeeShaoYajingUpdate : Form
    {
        ModelFEE_SHAOYAJING modelFEE_SHAOYAJING = new ModelFEE_SHAOYAJING();
        public ModelFEE_SHAOYAJING _ModelFEE_SHAOYAJING
        {
            get
            {
                return modelFEE_SHAOYAJING;
            }
            set
            {
                modelFEE_SHAOYAJING = value;
            }
        }
        DalFEE_SHAOYAJING aDalFEE_SHAOYAJING = new DalFEE_SHAOYAJING();
        DalCFG_CODE dalCFG_CODE = new DalCFG_CODE();
        public FormFeeShaoYajingUpdate()
        {
            InitializeComponent();
         
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
           
        }

        private void FormFeeShaoYajingUpdate_Load(object sender, EventArgs e)
        {
            //押金类型
            DataSet cfgCodeSet = this.dalCFG_CODE.GetList("TYPE = '押金类型'");
            this.comboBox1Type.DisplayMember = "CODE_VALUE";
            this.comboBox1Type.ValueMember = "CODE_VALUE";
            this.comboBox1Type.DataSource = cfgCodeSet.Tables[0];
            this.comboBox1Type.Text = this._ModelFEE_SHAOYAJING.TPYE; 

            //更新
            if (this.aDalFEE_SHAOYAJING.Exists(this._ModelFEE_SHAOYAJING.SEQID))
            {
                this.textBox4Opera.Text = this._ModelFEE_SHAOYAJING.OPERA_MAN;
                this.userControlRentContractInfo1._SHOW_BUTTON = false;
                this.userControlRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID;
               
               



                DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                ModelRENT_CONTRACT _ModelRENT_CONTRACT = _DalRENT_CONTRACT.GetModel(this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID);
                this.userControlRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID, _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);

                
            }   
            else
            {
                //如果添加时有合同号.则自动加载
                if (this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID > 0)
                {
                    this.userControlRentContractInfo1._CONTRACT_SEQID = this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID;
                    DalRENT_CONTRACT _DalRENT_CONTRACT = new DalRENT_CONTRACT();
                    ModelRENT_CONTRACT _ModelRENT_CONTRACT = _DalRENT_CONTRACT.GetModel(this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID);
                    this.userControlRentContractInfo1.selectedOk(_ModelRENT_CONTRACT.SEQID, _ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, _ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                }
               //登陆用户
                this.textBox4Opera.Text = Manager.getInstance().userAccount.USERNAME;
                this.userControlRentContractInfo1._SHOW_BUTTON = true;
                this._ModelFEE_SHAOYAJING.OPERA_TIME = DateTime.Now;
                this._ModelFEE_SHAOYAJING.RECORD_TIME = DateTime.Now;


               
                 
            }

           

 
            this.comboBox1Status.DisplayMember = "Name";
            this.comboBox1Status.ValueMember = "IntId";
            this.comboBox1Status.DataSource = Constant.FEE_YAJIN_STATUS_LIST;
            if (this._ModelFEE_SHAOYAJING.STATUS != 0)
            {
                this.comboBox1Status.SelectedValue = this._ModelFEE_SHAOYAJING.STATUS;
            }
            else {
                this.comboBox1Status.SelectedValue = Constant.FEE_YAJIN_STATUS_YISHAO;
            }


            this.textBox2Money.Text = this._ModelFEE_SHAOYAJING.MONEY+"";
           
            this.textBox1Note.Text = this._ModelFEE_SHAOYAJING.NOTE;
            this.dateTimePicker1MoneyTime.Text = this._ModelFEE_SHAOYAJING.OPERA_TIME.ToString(StringUtils.YYYYMMDD);
            this.dateTimePicker2Record.Text = this._ModelFEE_SHAOYAJING.RECORD_TIME.ToString(StringUtils.YYYYMMDD);

           
        }
         private Boolean check()
        {
            try
            {//dalRENT_CUSTOM

                if (this.userControlRentContractInfo1._CONTRACT_SEQID <= 0)
                {
                    throw new Exception("请先选择合同信息");

                }
                StringUtils.checkIsDecimal(this.textBox2Money.Text, "押金金额");

                StringUtils.checkText(this.textBox1Note.Text, 0, 300, "备注信息");

                int selectStatus = (int)this.comboBox1Status.SelectedValue;
                if (selectStatus == Constant.FEE_YAJIN_STATUS_YITEI || selectStatus == Constant.FEE_YAJIN_STATUS_YITEIBUFENG)
                {
                    throw new Exception("不允许直接修改为已退或已退部份.");
                }

                if (Constant.FEE_YAJIN_STATUS_YISHAO == selectStatus && Decimal.Parse(textBox2Money.Text) <= 0)
                {
                    throw new Exception("你还没有输入押金金额.");
                }

                if (Constant.FEE_YAJIN_STATUS_BUSHAO == selectStatus && Decimal.Parse(textBox2Money.Text) != 0)
                {
                    throw new Exception("选择免收款时不能输有押金金额,请输入0");
                }
               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        
        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._ModelFEE_SHAOYAJING.MONEY = Convert.ToDecimal(this.textBox2Money.Text);
            this._ModelFEE_SHAOYAJING.NOTE = this.textBox1Note.Text;
            this._ModelFEE_SHAOYAJING.OPERA_MAN = this.textBox4Opera.Text;
            this._ModelFEE_SHAOYAJING.OPERA_TIME = this.dateTimePicker1MoneyTime.Value;
            this._ModelFEE_SHAOYAJING.RECORD_TIME = this.dateTimePicker2Record.Value;
            this._ModelFEE_SHAOYAJING.STATUS = Convert.ToInt32(this.comboBox1Status.SelectedValue);
            this._ModelFEE_SHAOYAJING.RENT_CONTRACT_SEQID = this.userControlRentContractInfo1._CONTRACT_SEQID;

            this._ModelFEE_SHAOYAJING.TPYE = this.comboBox1Type.Text;


            if (aDalFEE_SHAOYAJING.Exists(this._ModelFEE_SHAOYAJING.SEQID))
            {
                aDalFEE_SHAOYAJING.Update(this._ModelFEE_SHAOYAJING);
            }
            else
            {
                aDalFEE_SHAOYAJING.Add(this._ModelFEE_SHAOYAJING);
            }
            //触发代理
            oneRefreshParentData(1);
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //代理
        public delegate void RefreshParentData(int page);
        public RefreshParentData oneRefreshParentData;

        

    }
}
